/*
 * Created by SharpDevelop.
 * User: MyWife
 * Date: 1/1/2003
 * Time: 12:10 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using popLib;
using System.IO;
using Microsoft.Win32;
using System.Runtime.InteropServices;

namespace SlideShowGadget
{
	/// <summary>
	/// Description of SlideShowPanel.
	/// </summary>
	public partial class SlideShowPanel : SidebarControl
	{
		List<string> images = new List<string>();
		int index = -1;
		Start.WallpaperStyle wallStyle;
		bool applyBackground = false;
		
		public SlideShowPanel(SidebarGadget sidebarGadget):base(sidebarGadget)
		{
			InitializeComponent();
			
			pbx_pic.Controls.Add( next );
			pbx_pic.Controls.Add( play );
			pbx_pic.Controls.Add( previous );
			pbx_pic.Controls.Add( stop );
			pbx_pic.Controls.Add( opt );
			
			MouseLeaveFunc( opt, new EventArgs() );
			MouseLeaveFunc( next, new EventArgs() );
			MouseLeaveFunc( play, new EventArgs() );
			MouseLeaveFunc( previous, new EventArgs() );
			MouseLeaveFunc( stop, new EventArgs() );
		}
		
		public override void StartComponent(){
			Hashtable options = sidebarGadget.Options;
					
			images.Clear();
			if( Directory.Exists( options[Start.optionsType.StartupDirectory.ToString()].ToString() ) ){
				images.AddRange( FileLib.getFileList( options[Start.optionsType.StartupDirectory.ToString()].ToString(), "*.jpg", true, FileLib.SearchOption.FileOnly ) );
				images.AddRange( FileLib.getFileList( options[Start.optionsType.StartupDirectory.ToString()].ToString(), "*.png", true, FileLib.SearchOption.FileOnly ) );
			}
			
			try{
				Random ran = new Random();
				
				for( int i=DateTime.Now.Second; i<= 60 ;i++)
					index = ran.Next(0, images.Count-1);
				
				timer1.Interval   = Convert.ToInt16( options[Start.optionsType.DelayTimer.ToString()] ) * 1000;
			}catch{ 
				index = 0;
				timer1.Interval = 30000; 
			}
			
			
			switch( options[Start.optionsType.WallpaperStyle+""].ToString().ToLower() ){
				case "centered": wallStyle = Start.WallpaperStyle.Centered; break;
				case "stretched": wallStyle = Start.WallpaperStyle.Stretched; break;
				case "autoscaled": wallStyle = Start.WallpaperStyle.AutoScaled; break;
				default:
				case "tiled": wallStyle = Start.WallpaperStyle.Tiled; break;
			}
			
			setBackgroundStyle();
			
			applyBackground = options[ Start.optionsType.SetAsWallpager+"" ]+"" == "1" ? true : false;
						
			if(  images.Count > 0 ){
				timer1.Stop();
				timer1.Start();
				viewImage(true);
			}
		}
		
		void viewImage(bool next){
			if(  images.Count == 0 ) return;
			if( next ){
				index++;
				if( index >= images.Count ) index = 0;
			}else{
				index--;
				if( index < 0 ) index = images.Count-1;
			}
			pbx_pic.Image = ImageLib.createThumbnailImage( Image.FromFile( images[index] ), pbx_pic.Width, pbx_pic.Height );
			
			if(applyBackground) 
				setBackground(images[index]);
			
			GC.Collect();
		}
		
		void setBackground(string imageFile){
			
			Image image = Image.FromFile(imageFile);
			
			switch( wallStyle ){
				case Start.WallpaperStyle.Stretched:
					image = ImageLib.reSize( image, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height );
					break;
				case Start.WallpaperStyle.AutoScaled:
					image = ImageLib.createThumbnailImage( image, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height );
					break;
				case Start.WallpaperStyle.FullScaled:
					image = ImageLib.createThumbnailImage( image, 
					                                      Screen.PrimaryScreen.Bounds.Width,
					                                      Screen.PrimaryScreen.Bounds.Height );
					
					if( image.Height < Screen.PrimaryScreen.Bounds.Height ){
						image = Image.FromFile(imageFile);
						image = ImageLib.createThumbnailImage( image,
					                                      Screen.PrimaryScreen.Bounds.Width,
					                                      Screen.PrimaryScreen.Bounds.Width );
					}
					break;
			}
			
			imageFile = FileLib.getFullPath("new.bmp");
			image.Save(imageFile, System.Drawing.Imaging.ImageFormat.Bmp);
			image.Dispose();
			
			SystemParametersInfo(20, 1, imageFile, 0x2 );
		}
		
		void setBackgroundStyle(){
			RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
			switch( wallStyle )
			{
			    case Start.WallpaperStyle.Stretched :
			        key.SetValue(@"WallpaperStyle", "2") ; 
			        key.SetValue(@"TileWallpaper", "0") ;
			        break;
			    case Start.WallpaperStyle.AutoScaled :
			    case Start.WallpaperStyle.FullScaled :
			    case Start.WallpaperStyle.Centered :			        
			        key.SetValue(@"WallpaperStyle", "1") ; 
			        key.SetValue(@"TileWallpaper", "0") ; 
			        break;
			    case Start.WallpaperStyle.Tiled :
			        key.SetValue(@"WallpaperStyle", "1") ; 
			        key.SetValue(@"TileWallpaper", "1") ;
			        break;
			}
		}
		
		public bool ThumbnailCallback()
		{
			return true;
		}
				
		void Timer1Tick(object sender, EventArgs e)
		{
			viewImage(true);
		}
		
		
		
		void ControlEvent(object sender, EventArgs e)
		{
			switch( ((Control)sender).Name ){
				case "pbx_pic": System.Diagnostics.Process.Start(images[index]); break;
				case "next": 
					viewImage(true); 
					if(timer1 != null ){
						timer1.Stop();
						timer1.Start();
					}
					break;
				case "previous": 
					viewImage(false); 
					if(timer1 != null ){
						timer1.Stop();
						timer1.Start();
					}
					break;
				case "play": if(timer1 != null ) timer1.Start(); break;
				case "stop": if(timer1 != null ) timer1.Stop(); break;
				case "opt": sidebarGadget.ViewOptions(); break;
			}
		}
			
				
		void MouseEnterFunc(object sender, EventArgs e)
		{
			Panel tmp = (Panel)sender;			
			tmp.BackgroundImage = (Image)tmp.Tag;
		}
		
		void MouseLeaveFunc(object sender, EventArgs e)
		{
			Panel tmp = (Panel)sender;
			if(tmp.Tag==null) tmp.Tag = tmp.BackgroundImage;			
			tmp.BackgroundImage = ImageLib.changeImageOpacity( (Image)tmp.Tag, 0.2F );
		}
		
		[DllImport("user32.dll", CharSet=CharSet.Auto)]
		public static extern int SystemParametersInfo (int uAction , int uParam , string lpvParam , int fuWinIni) ;
	}
}
